package com.sanji.sjmallmodule.account;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sanji.sjmallapp.BaseActivity;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallconfig.ConfigAPI;
import com.sanji.sjmallconfig.ConfigConts;
import com.sanji.sjmallmanager.http.NoHttpCallBack;
import com.sanji.sjmallmanager.http.NoHttpListener;
import com.sanji.sjmallutils.Tools;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 用户信息
 */
public class PersonInfoActivity extends BaseActivity implements View.OnClickListener, NoHttpListener {
    @BindView(R.id.common_back_img)
    ImageView commonBackImg;
    @BindView(R.id.common_left_tv)
    TextView commonLeftTv;
    @BindView(R.id.store_name_tv)
    TextView storeNameTv;
    @BindView(R.id.local_area_tv)
    TextView localAreaTv;
    @BindView(R.id.detailed_address_tv)
    TextView detailedAddressTv;
    @BindView(R.id.person_info_recipt_person_tv)
    TextView reciptPersonTv;
    @BindView(R.id.recipt_person_phone_tv)
    TextView reciptPersonPhoneTv;
    @BindView(R.id.channel_code_tv)
    TextView channelCodeTv;
    @BindView(R.id.bank_account_tv)
    TextView bankAccountTv;
    @BindView(R.id.bank_card_number_tv)
    TextView bankCardNumberTv;
    @BindView(R.id.bank_card_name_tv)
    TextView bankCardNameTv;
    @BindView(R.id.improve_info_btn)
    Button improveInfoBtn;
    private Request<JSONObject> request;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_info);
        ButterKnife.bind(this);
        initView();
        requestUserInfo();
    }

    public void initView() {
        commonLeftTv.setText(getString(R.string.person_info));
        commonBackImg.setOnClickListener(this);
        improveInfoBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back_img:
                finish();
                break;
            case R.id.improve_info_btn:
                //完善信息
                intent = new Intent(this, ImproveActivity.class);
                startActivity(intent);
                break;
        }

    }

    /**
     * 网络请求用户信息
     **/
    public void requestUserInfo() {
        String userId = myShare.getString(ConfigConts.USER_ID, null);
        String url = ConfigAPI.USER_INFO_URL + userId;
        request = NoHttp.createJsonObjectRequest(url, RequestMethod.GET);
        request.setCancelSign(ConfigConts.USER_ID.hashCode());
        NoHttpCallBack.getInstance().add(mCtx, ConfigConts.USER_ID.hashCode(), request, this, true, true);
    }

    @Override
    public void onSuccessed(int what, Response response) {
        if (what == ConfigConts.USER_ID.hashCode()) {
            int responseCode = response.getHeaders().getResponseCode();
            JSONObject object;
            if (responseCode == 200) {
                object = (JSONObject) response.get();
                String status = object.optString(ConfigConts.STATUS_STRING);
                if (status.equals(ConfigConts.STATUS_SUCCESS)) {
                    JSONObject result = object.optJSONObject("result");
                    storeNameTv.setText(result.optString("account"));//店铺名
                    localAreaTv.setText(result.optString("namePath"));//所在区域
                    detailedAddressTv.setText(result.optString("address"));//详细地址
                    reciptPersonTv.setText(result.optString("trueName"));//收件人
                    reciptPersonPhoneTv.setText(result.optString("mobile"));//收件人电话
                    channelCodeTv.setText(result.optString("channelCoding"));//渠道编码
                    bankAccountTv.setText(result.optString("depositBank"));//开户行
                    bankCardNumberTv.setText(result.optString("bankCard"));//银行卡号
                    bankCardNameTv.setText(result.optString("bancardname"));//开户行名字
                } else if (status.equals(ConfigConts.STATUS_ERROR)) {
                    String errorSmg = object.optString(ConfigConts.ERRORMSG);
                    Tools.toastTools(mCtx, errorSmg);
                }
            }
        }

    }

    @Override
    public void onFailed(int what, Response response) {
        Tools.toastTools(mCtx, getString(R.string.false_message));
    }
}
